package com.tbit.uqbike.service.business.impl;

import com.tbit.uqbike.constant.DisPatchConstant;
import com.tbit.uqbike.constant.enums.OrderStatusEnum;
import com.tbit.uqbike.object.pojo.vo.ReturnBikeMsgStatVO;
import com.tbit.uqbike.object.pojo.vo.ReturnBikeStatVO;
import com.tbit.uqbike.service.business.ReturnBikeStatService;
import com.tbit.uqbike.service.business.SLSLogService;
import com.tbit.uqbike.webmanager.dao.log.ReturnBikeMsgLogDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @AUTHOR: wei.xing
 * @DATE: 2023-05-17 15:55
 **/
@Service
public class ReturnBikeStatServiceImpl implements ReturnBikeStatService {

    @Autowired
    private SLSLogService slsLogService;

    @Autowired
    private ReturnBikeMsgLogDao returnBikeMsgLogDao;

    @Override
    public List<ReturnBikeStatVO> getNoReturnBikeStat(Integer brandId, Integer accountId, Long startTime, Long endTime) {
        List<ReturnBikeStatVO> returnBikeStatList = slsLogService.getNoReturnBikeStatList(brandId, accountId, startTime, endTime);
        Map<String, Long> returnBikeStatMap = returnBikeStatList.stream().collect(Collectors.toMap(v -> v.getReturnBikeType(), v -> v.getReturnBikeCount()));

        List<ReturnBikeStatVO> list = new ArrayList<>();
        for (OrderStatusEnum status : OrderStatusEnum.values()) {
            Integer returnBikeType = status.getIndex();
            if (!(returnBikeType.equals(DisPatchConstant.DISPATCH_PARK)
                    || returnBikeType.equals(DisPatchConstant.DISPATCH_RUN)
                    || returnBikeType.equals(DisPatchConstant.DISPATCH_NO_CARSTATUS)
                    || returnBikeType.equals(DisPatchConstant.DISPATCH_BLE)
                    || returnBikeType.equals(DisPatchConstant.DISPATCH_NFC)
                    || returnBikeType.equals(DisPatchConstant.DISPATCH_HELMET_EX))) {
                Long returnBikeCount = returnBikeStatMap.get(returnBikeType.toString());
                returnBikeCount = returnBikeCount == null ? 0 : returnBikeCount;
                list.add(new ReturnBikeStatVO(returnBikeType.toString(), returnBikeCount));
            }
        }
        return list;
    }

    @Override
    public ReturnBikeMsgStatVO getReturnBikeMsgStat(Integer brandId, Integer accountId, Long startTime, Long endTime) {
        return returnBikeMsgLogDao.getReturnBikeMsgStat(brandId, accountId, new Date(startTime), new Date(endTime));
    }
}
